<HTML>
<HEAD>
<TITLE>[Chapter 10] 10.6 CheckboxMenuItem</TITLE>
<META NAME="author" CONTENT="John Zukowski">
<META NAME="date" CONTENT="Thu Jul 31 14:46:30 1997">
<META NAME="form" CONTENT="html">
<META NAME="metadata" CONTENT="dublincore.0.1">
<META NAME="objecttype" CONTENT="book part">
<META NAME="otheragent" CONTENT="gmat dbtohtml">
<META NAME="publisher" CONTENT="O'Reilly &amp; Associates, Inc.">
<META NAME="source" CONTENT="SGML">
<META NAME="subject" CONTENT="Java AWT">
<META NAME="title" CONTENT="Java AWT">
<META HTTP-EQUIV="Content-Script-Type" CONTENT="text/javascript">
</HEAD>
<body vlink="#551a8b" alink="#ff0000" text="#000000" bgcolor="#FFFFFF" link="#0000ee">

<DIV CLASS=htmlnav>
<H1><a href='index.htm'><IMG SRC="gifs/smbanner.gif"
     ALT="Java AWT" border=0></a></H1>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch10_05.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><B><FONT FACE="ARIEL,HELVETICA,HELV,SANSERIF" SIZE="-1">Chapter 10<br>Would You Like to Choose from the Menu?</FONT></B></TD>
<td width=172 align=right valign=top><A HREF="ch10_07.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
</table>

&nbsp;
<hr align=left width=515>
</DIV>
<DIV CLASS=sect1>
<h2 CLASS=sect1><A CLASS="TITLE" NAME="JAWT-CH-10-SECT-6">10.6 CheckboxMenuItem</A></h2>

<P CLASS=para>
<A NAME="CH10.MENU2"></A>The <tt CLASS=literal>CheckboxMenuItem</tt> is a subclass 
of <tt CLASS=literal>MenuItem</tt> that can be toggled. 
It is similar to a <tt CLASS=literal>Checkbox</tt> 
but appears on a <tt CLASS=literal>Menu</tt>. The 
appearance depends upon the platform. There may or may not be a visual 
indicator next to the choice. However, when the <tt CLASS=literal>MenuItem</tt> 
is selected (<tt CLASS=literal>true</tt>), a checkmark 
or some similar graphic will be displayed next to the label. 

<P CLASS=para>
There is no way to put <tt CLASS=literal>CheckboxMenuItem</tt> 
components into a <tt CLASS=literal>CheckboxGroup</tt> 
to form a radio menu group. 

<P CLASS=para>
An example of a <tt CLASS=literal>CheckboxMenuItem</tt> 
is the Show Java Console menu item in Netscape Navigator. 

<DIV CLASS=sect2>
<h3 CLASS=sect2><A CLASS="TITLE" NAME="JAWT-CH-10-SECT-6.1">CheckboxMenuItem Methods</A></h3>Constructors

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry><I CLASS=emphasis>public CheckboxMenuItem (String label) </I><br>
<DD>

<P CLASS=para>
The first <tt CLASS=literal>CheckboxMenuItem</tt> 
constructor creates a <tt CLASS=literal>CheckboxMenuItem</tt> 
with no label displayed next to the check toggle. The initial value of 
the <tt CLASS=literal>CheckboxMenuItem</tt> is <tt CLASS=literal>false</tt>. 
To set the label at a later time, use <tt CLASS=literal>setLabel()</tt>. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public CheckboxMenuItem (String label) </I><br>
<DD>

<P CLASS=para>
The next <tt CLASS=literal>CheckboxMenuItem</tt> constructor 
creates a <tt CLASS=literal>CheckboxMenuItem</tt> 
with <tt CLASS=literal>label</tt> displayed next to 
the check toggle. The initial value of the <tt CLASS=literal>CheckboxMenuItem</tt> 
is <tt CLASS=literal>false</tt>. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public CheckboxMenuItem (String label, boolean state) </I><br>
<DD>

<P CLASS=para>
The final <tt CLASS=literal>CheckboxMenuItem</tt> 
constructor creates a <tt CLASS=literal>CheckboxMenuItem</tt> 
with <tt CLASS=literal>label</tt> displayed next to 
the check toggle. The initial value of the <tt CLASS=literal>CheckboxMenuItem</tt> 
is <tt CLASS=literal>state</tt>. </DL>
Selection

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry><I CLASS=emphasis>public boolean getState () </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>getState()</tt> method retrieves 
the current state of the <tt CLASS=literal>CheckboxMenuItem</tt>. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public void setState (boolean condition) </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>setState()</tt> method changes 
the current state of the <tt CLASS=literal>CheckboxMenuItem</tt> 
to <tt CLASS=literal>condition</tt>. When <tt CLASS=literal>true</tt>, 
the <tt CLASS=literal>CheckboxMenuItem</tt> will have 
the toggle checked. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public Object[] getSelectedObjects () <img src="gifs/bstar.gif" alt="(New)" border=0> </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>getSelectedItems()</tt> method 
returns the currently selected item as an <tt CLASS=literal>Object</tt> 
array. This method, which is required by the <tt CLASS=literal>ItemSelectable</tt> 
interface, allows you to use the same methods to retrieve the selected 
items of any <tt CLASS=literal>Checkbox</tt>, <tt CLASS=literal>Choice</tt>, 
or <tt CLASS=literal>List</tt>. The array has at most 
one element, which contains the label of the selected item; if no item 
is selected, <tt CLASS=literal>getSelectedItems()</tt> 
returns <tt CLASS=literal>null</tt>. </DL>
Miscellaneous methods

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry><I CLASS=emphasis>public synchronized void addNotify ()  </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>addNotify()</tt> method creates 
the <tt CLASS=literal>CheckboxMenuItem</tt> 
peer. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public String paramString ()</I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>paramString()</tt> method of 
<tt CLASS=literal>CheckboxMenuItem</tt> should be 
protected like other <tt CLASS=literal>paramString()</tt> 
methods. However, it is public, so you have access to it. When you call 
the <tt CLASS=literal>toString()</tt> method of a 
<tt CLASS=literal>CheckboxMenuItem</tt>, the default 
<tt CLASS=literal>toString()</tt> method of <tt CLASS=literal>MenuComponent</tt> 
is called. This in turn calls <tt CLASS=literal>paramString()</tt> 
which builds up the string to display. At the <tt CLASS=literal>CheckboxMenuItem</tt> 
level, the current state of the object is appended to the output. If the 
constructor for the <tt CLASS=literal>CheckboxMenuItem</tt> 
was <tt CLASS=literal>new CheckboxMenuItem(`File`)</tt> 
the results would be: </DL>
<DIV CLASS=screen>
<P>
<PRE>
java.awt.CheckboxMenuItem[label=File,state=false]
</PRE>
</DIV>

</DIV>

<DIV CLASS=sect2>
<h3 CLASS=sect2><A CLASS="TITLE" NAME="JAWT-CH-10-SECT-6.2">CheckboxMenuItem Events</A></h3><A NAME="CH10.EVENT2"></A><A NAME="CH10.EVENT2A"></A><A NAME="CH10.EVENT2B"></A> Event handling

<P CLASS=para>
A <tt CLASS=literal>CheckboxMenuItem</tt> generates 
an <tt CLASS=literal>ACTION_EVENT</tt> when it is 
selected. The argument to <tt CLASS=literal>action()</tt> 
is the label of the <tt CLASS=literal>CheckboxMenuItem</tt>, 
like the method provided by <tt CLASS=literal>MenuItem</tt>, 
not the state of the <tt CLASS=literal>CheckboxMenuItem</tt> 
as used in <tt CLASS=literal>Checkbox</tt>. The 
target of the <tt CLASS=literal>ACTION_EVENT</tt> 
is the <tt CLASS=literal>Frame</tt> containing the 
menu. You cannot subclass <tt CLASS=literal>CheckboxMenuItem</tt> 
and handle the <tt CLASS=literal>Event</tt> within 
the subclass unless you override <tt CLASS=literal>postEvent()</tt>. Listeners and 1.1 event handling

<P CLASS=para>
With the Java 1.1 event model, you register listeners, which are told when 
the event happens. 

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry><I CLASS=emphasis>public void addItemListener(ItemListener listener) <img src="gifs/bstar.gif" alt="(New)" border=0> </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>addItemListener()</tt> method 
registers <tt CLASS=literal>listener</tt> as an object 
that is interested in being notified when an <tt CLASS=literal>ItemEvent</tt> 
passes through the <tt CLASS=literal>EventQueue</tt> 
with this <tt CLASS=literal>CheckboxMenuItem</tt> 
as its target. When these item events occur, the <tt CLASS=literal>listener.itemStateChanged()</tt> 
method is called. Multiple listeners can be registered. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public void removeItemListener(ItemListener listener) <img src="gifs/bstar.gif" alt="(New)" border=0> </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>removeItemListener()</tt> method 
removes <tt CLASS=literal>listener</tt> as a interested 
listener. If <tt CLASS=literal>listener</tt> is not 
registered, nothing happens. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>protected void processEvent(AWTEvent e) <img src="gifs/bstar.gif" alt="(New)" border=0> </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>processEvent()</tt> method receives 
every <tt CLASS=literal>AWTEvent</tt> with this <tt CLASS=literal>CheckboxMenuItem</tt> 
as its target. <tt CLASS=literal>processEvent()</tt> 
then passes it along to any listeners for processing. When you subclass 
<tt CLASS=literal>CheckboxMenuItem</tt>, overriding 
<tt CLASS=literal>processEvent()</tt> allows you to 
process all events yourself, before sending them to any listeners. In a 
way, overriding <tt CLASS=literal>processEvent()</tt> 
is like overriding <tt CLASS=literal>postEvent()</tt> 
using the 1.0 event model. 

<P CLASS=para>
If you override <tt CLASS=literal>processEvent()</tt>, 
remember to call <tt CLASS=literal>super.processEvent(e)</tt> 
last to ensure that regular event processing can occur. If you want to 
process your own events, it's a good idea to call <tt CLASS=literal>enableEvents() </tt>to ensure that events are delivered, even in the 
absence of registered listeners. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>protected void processItemEvent(ItemEvent e) <img src="gifs/bstar.gif" alt="(New)" border=0> </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>processItemEvent()</tt> method 
receives every <tt CLASS=literal>ItemEvent</tt> with 
this <tt CLASS=literal>CheckboxMenuItem</tt> as its 
target. <tt CLASS=literal>processItemEvent()</tt> 
then passes it along to any listeners for processing. When you subclass 
<tt CLASS=literal>CheckboxMenuItem</tt>, overriding 
<tt CLASS=literal>processItemEvent()</tt> allows you 
to process all item events yourself, before sending them to any listeners. 
In a way, overriding <tt CLASS=literal>processItemEvent()</tt> 
is like overriding <tt CLASS=literal>action() </tt>using 
the 1.0 event model. 

<P CLASS=para>
If you override <tt CLASS=literal>processItemEvent()</tt>, 
remember to call the method <tt CLASS=literal>super.processItemEvent(e)</tt> 
last to ensure that regular event processing can occur. If you want to 
process your own events, it's a good idea to call <tt CLASS=literal>enableEvents()</tt> 
to ensure that events are delivered even in the absence of registered listeners. </DL>
</DIV>

</DIV>


<DIV CLASS=htmlnav>

<P>
<HR align=left width=515>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch10_05.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><a href="index.htm"><img src='gifs/txthome.gif' border=0 alt='Home'></a></td>
<td width=172 align=right valign=top><A HREF="ch10_07.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
<tr>
<td width=172 align=left valign=top>Menu</td>
<td width=171 align=center valign=top><a href="index/idx_a.htm"><img src='gifs/index.gif' alt='Book Index' border=0></a></td>
<td width=172 align=right valign=top>MenuBar</td>
</tr>
</table>
<hr align=left width=515>

<IMG SRC="gifs/smnavbar.gif" USEMAP="#map" BORDER=0> 
<MAP NAME="map"> 
<AREA SHAPE=RECT COORDS="0,0,108,15" HREF="../javanut/index.htm"
alt="Java in a Nutshell"> 
<AREA SHAPE=RECT COORDS="109,0,200,15" HREF="../langref/index.htm" 
alt="Java Language Reference"> 
<AREA SHAPE=RECT COORDS="203,0,290,15" HREF="../awt/index.htm" 
alt="Java AWT"> 
<AREA SHAPE=RECT COORDS="291,0,419,15" HREF="../fclass/index.htm" 
alt="Java Fundamental Classes"> 
<AREA SHAPE=RECT COORDS="421,0,514,15" HREF="../exp/index.htm" 
alt="Exploring Java"> 
</MAP>
</DIV>

</BODY>
</HTML>
